package com.jzo2o.customer.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jzo2o.common.model.CurrentUser;
import com.jzo2o.common.model.PageResult;
import com.jzo2o.common.utils.UserContext;
import com.jzo2o.common.model.PageResult;
import com.jzo2o.customer.enums.CertificationStatusEnum;
import com.jzo2o.customer.mapper.AgencyCertificationMapper;
import com.jzo2o.customer.model.domain.AgencyCertification;
import com.jzo2o.customer.model.domain.AgencyCertificationAudit;
import com.jzo2o.customer.model.dto.AgencyCertificationUpdateDTO;
import com.jzo2o.customer.model.dto.request.AgencyCertificationAuditAddReqDTO;
import com.jzo2o.customer.model.dto.response.RejectReasonResDTO;
import com.jzo2o.customer.service.AgencyCertificationAuditService;
import com.jzo2o.customer.model.dto.request.AgencyCertificationAuditPageQueryReqDTO;
import com.jzo2o.customer.service.IAgencyCertificationService;
import com.jzo2o.mysql.utils.PageHelperUtils;
import com.jzo2o.mysql.utils.PageHelperUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 机构认证信息表 服务实现类
 * </p>
 *
 * @author itcast
 * @since 2023-09-06
 */
@Service
public class AgencyCertificationServiceImpl extends ServiceImpl<AgencyCertificationMapper, AgencyCertification> implements IAgencyCertificationService {

     @Resource
     private AgencyCertificationAuditService agencyCertificationAuditService;


    /**
     * 根据机构id更新
     *
     * @param agencyCertificationUpdateDTO 机构认证更新模型
     */
    @Override
    public void updateByServeProviderId(AgencyCertificationUpdateDTO agencyCertificationUpdateDTO) {
        LambdaUpdateWrapper<AgencyCertification> updateWrapper = Wrappers.<AgencyCertification>lambdaUpdate()
                .eq(AgencyCertification::getId, agencyCertificationUpdateDTO.getId())
                .set(AgencyCertification::getCertificationStatus, agencyCertificationUpdateDTO.getCertificationStatus())
                .set(ObjectUtil.isNotEmpty(agencyCertificationUpdateDTO.getName()), AgencyCertification::getName, agencyCertificationUpdateDTO.getName())
                .set(ObjectUtil.isNotEmpty(agencyCertificationUpdateDTO.getIdNumber()), AgencyCertification::getIdNumber, agencyCertificationUpdateDTO.getIdNumber())
                .set(ObjectUtil.isNotEmpty(agencyCertificationUpdateDTO.getLegalPersonName()), AgencyCertification::getLegalPersonName, agencyCertificationUpdateDTO.getLegalPersonName())
                .set(ObjectUtil.isNotEmpty(agencyCertificationUpdateDTO.getLegalPersonIdCardNo()), AgencyCertification::getLegalPersonIdCardNo, agencyCertificationUpdateDTO.getLegalPersonIdCardNo())
                .set(ObjectUtil.isNotEmpty(agencyCertificationUpdateDTO.getBusinessLicense()), AgencyCertification::getBusinessLicense, agencyCertificationUpdateDTO.getBusinessLicense())
                .set(ObjectUtil.isNotEmpty(agencyCertificationUpdateDTO.getCertificationTime()), AgencyCertification::getCertificationTime, agencyCertificationUpdateDTO.getCertificationTime());
        super.update(updateWrapper);
    }

    @Override
    public void addCertification(AgencyCertificationAuditAddReqDTO agencyCertificationAuditAddReqDTO) {
        Long id = UserContext.getCurrentUser().getId();
        if(ObjectUtil.isEmpty(agencyCertificationAuditAddReqDTO)){
            throw new RuntimeException("认证信息不能为空!");
        }
        AgencyCertification bean = BeanUtil.toBean(agencyCertificationAuditAddReqDTO, AgencyCertification.class);
        bean.setId(id);
        bean.setCertificationStatus(1);
        bean.setCertificationTime(LocalDateTime.now());
        bean.setCreateTime(LocalDateTime.now());
        bean.setUpdateTime(LocalDateTime.now());
        save(bean);


        AgencyCertificationAudit agencyAudit = BeanUtil.toBean(bean, AgencyCertificationAudit.class, CopyOptions.create().setIgnoreProperties("id"));

        agencyAudit.setServeProviderId(id);
        agencyAudit.setAuditStatus(0);


        agencyCertificationAuditService.save(agencyAudit);
    }

    @Override
    public RejectReasonResDTO failMsg() {
        CurrentUser currentUser = UserContext.getCurrentUser();
        Long id = currentUser.getId();

//        String idString = "";

        AgencyCertificationAudit one = agencyCertificationAuditService.lambdaQuery().eq(AgencyCertificationAudit::getServeProviderId, id).one();


        String rejectReason = one.getRejectReason();
        RejectReasonResDTO rejectReasonResDTO = new RejectReasonResDTO();
        rejectReasonResDTO.setRejectReason(rejectReason);
        return rejectReasonResDTO;
    }

    @Override
    public PageResult<AgencyCertification> pageQueryWorker(AgencyCertificationAuditPageQueryReqDTO agencyDTO) {
        return PageHelperUtils.selectPage(agencyDTO, () -> {
            return lambdaQuery()
                    .eq(ObjectUtil.isNotEmpty(agencyDTO.getCertificationStatus()), AgencyCertification::getCertificationStatus, agencyDTO.getCertificationStatus())
                    .eq(StrUtil.isNotEmpty(agencyDTO.getLegalPersonName()),AgencyCertification::getLegalPersonName, agencyDTO.getLegalPersonName())
                    .eq(StrUtil.isNotEmpty(agencyDTO.getName()), AgencyCertification::getName, agencyDTO.getName())
                                .list();
        });
    }


}
